AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include( "shared.lua" )

function ENT:Initialize()
	-- Set model.
	self.Entity:SetModel( self:GetKeyValue( "model", "models/props_combine/CombineThumper002.mdl" ) )
	
	-- Initialise physics.
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_VPHYSICS )
	
	-- Freeze!
	local phys = self:GetPhysicsObject()
	if ( phys:IsValid() ) then
		phys:EnableMotion( false )
	end
	
	-- Set health.
	self:SetPropHealth(tonumber(self:GetKeyValue("health", 1500)), true)
	self:SetPropMaxHealth(self:GetPropHealth())
	
	self.Destroyed = false
end

function ENT:OnTakeDamage( info )
	if ( self:GetPropHealth() <= 0 ) then return end
	if ( GAMEMODE:CallHook( "GeneratorShouldTakeDamage", self, info ) == false ) then return end
	
	// Do less damage from bullets, etc.
	local damage = info:GetDamage()
	if !info:IsExplosionDamage() then
		damage = damage / 2
	end
	self:SetPropHealth(self:GetPropHealth() - damage, true)
	
	if ( self:GetPropHealth() <= 0 && !self.Destroyed ) then
		self.Destroyed = true
		
		GAMEMODE:CallHook( "GeneratorDestroyed", self )
		self:Fire( "FireUser1", "", 0 ) // We fire the User1 output when destroyed.
		
		// Clear the assault points.
		for k,v in pairs (ents.FindByName (self:GetKeyValue ("assaultpoint", "NOTANENTITY"))) do
			printd ("forcing clearing of "..tostring(v).."\n")
			v:SetKeyValue ("spawnflags", "1")
		end
	end
end
function ENT:IsDestroyed()
	return self.Destroyed
end

function ENT:GetKeyValue( key, default )
	if ( !self.KeyValues || !self.KeyValues[ key ] ) then return default end
	return self.KeyValues[ key ]
end
function ENT:KeyValue( key, value )
	self.KeyValues = self.KeyValues or {}
	self.KeyValues[ key ] = value
end
